﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class AdminControl_ImportProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String _proid = Request["proid"];
        this.proid.Value = _proid;
        ShoppingDataContext spdt = new ShoppingDataContext();
        var pro = from p in spdt.PRODUCTs
                  where p.ProductID == _proid
                  select p;
        if (pro.Count() <= 0) return;

        this.proname.Text = pro.First().ProName;
        this.usefor.Text = pro.First().usefor;
    }

    protected void save_Click(object sender, EventArgs e)
    {
        String proid = this.proid.Value;
        DateTime dt = DateTime.Today;
        String prosize = this.size.Text;
        String procolor = this.color.Text;
        ShoppingDataContext spdt = new ShoppingDataContext();

        var color = from p in spdt.COLORs
                    where p.Color1 == procolor
                    select p;
        if (color.Count() <= 0)
        {
            COLOR col = new COLOR();
            col.Color1 = procolor;
            col.Descript = procolor;
            spdt.COLORs.InsertOnSubmit(col);
            spdt.SubmitChanges();
        }

        var size = from p in spdt.SIZEs
                    where p.Size1 == prosize
                    select p;
        if (size.Count() <= 0)
        {
            SIZE siz = new SIZE();
            siz.Size1 = prosize;
            siz.Descript = prosize;
            spdt.SIZEs.InsertOnSubmit(siz);
            spdt.SubmitChanges();
        }


        PRO_IMPORT import = new PRO_IMPORT();
        import.ProductID = proid;
        import.size = prosize;
        import.color = procolor;
        import.importdate = dt;
        import.price = int.Parse(this.price.Text);
        import.number = int.Parse(this.number.Text);
        import.company = this.company.Text;
        spdt.PRO_IMPORTs.InsertOnSubmit(import);
        spdt.SubmitChanges();

        var prodes = from p in spdt.PRODUCT_DEs
                     where p.ProductID == import.ProductID
                        && p.Size == import.size
                        && p.Color == import.color
                     select p;

        if (prodes.Count() > 0)
        {
            PRODUCT_DE productdes = spdt.PRODUCT_DEs.Single(p => p.Product_DesID == prodes.First().Product_DesID);
            productdes.number = productdes.number + prodes.First().number;
            spdt.SubmitChanges();
        }
        else
        {
            
            var member = from p in spdt.PRODUCT_DEs
                     select p;
            int temp = 1;
            int flag1 = 0;
            int flag = 1;
            int ProId = 0;
            string tam = "PD";
            while (flag != 0)
            {
                foreach (var mem in member)
                {
                    string id = mem.Product_DesID.Substring(2, 4);
                    ProId = int.Parse(id);
                    if (ProId == temp)
                    {
                        temp++;
                        flag1 = 0;
                        break;
                    }

                    flag1 = 1;
                }
                if (flag1 == 1)
                {
                    flag = 0;
                    ProId = temp;
                }

            }
            for (int i = 1; i <= 4 - ProId.ToString().Length; i++)
            {
                tam = tam + "0";
            }
            tam = tam + ProId.ToString();

            string strFileNameWithPath = imageupload.PostedFile.FileName;
            string strExtensionName = System.IO.Path.GetExtension(strFileNameWithPath);
            string strFileName = System.IO.Path.GetFileName(strFileNameWithPath);

            strExtensionName = strExtensionName.ToLower();
            if (strExtensionName.Equals(".jpg") || strExtensionName.Equals(".gif") || strExtensionName.Equals(".png"))
            {
                imageupload.PostedFile.SaveAs(Server.MapPath("~/Images/ImagesForProduct/") + strFileName);
            }

            PRODUCT_DE productdes = new PRODUCT_DE();
            productdes.Product_DesID = tam;
            productdes.ProductID = proid;
            productdes.Size = prosize;
            productdes.Color = procolor;
            productdes.price = int.Parse(this.price.Text);
            productdes.number = int.Parse(this.number.Text);
            productdes.image = strFileName.Insert(0, "~/Images/ImagesForProduct/") ;
            productdes.discount = 0;

            spdt.PRODUCT_DEs.InsertOnSubmit(productdes);
            spdt.SubmitChanges();
        }
    }
}
